package com.qiqi.admin.basicdata.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiqi.basicdata.dto.ProcessDTO;
import com.qiqi.basicdata.entity.ProcessDO;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiqi.common.entity.PageEntity;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qiqi.basicdata.service.ProcessService;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QiQiDream
 * @since 2020-11-28
 */
@Api("相关接口")
@RestController
@RequestMapping("/process")
public class ProcessController {

    @Resource
    private ProcessService processService;

    @ApiOperation(value = "获取(列表)")
    @PostMapping("list")
    public PageEntity<ProcessDO> getProcessPage(@RequestBody ProcessDTO process) {
        LambdaQueryWrapper<ProcessDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(process.getType()),ProcessDO::getType,process.getType())
                .like(StringUtils.isNotBlank(process.getProcess()),ProcessDO::getProcess,process.getProcess())
                .orderBy(true,true,ProcessDO::getType).orderBy(true,true,ProcessDO::getOrders);
        IPage<ProcessDO> iPage = processService.page(new Page<>(process.getPage(),process.getCount()),queryWrapper);
        return new PageEntity<>(iPage.getTotal(),Convert.convert(new TypeReference<List<ProcessDO>>() {}, iPage.getRecords()));
    }

    @ApiOperation(value = "获取工序不分页(下拉框)")
    @GetMapping("/all")
    public List<ProcessDO> getList(){
        return processService.list();
    }

    @ApiOperation(value = "判断工序是否存在")
    @GetMapping("isExit")
    public Boolean isExit(@RequestParam String process){
        if(ObjectUtils.isEmpty(process)){
            return true;
        }
        List<ProcessDO> list = processService.list(new LambdaQueryWrapper<ProcessDO>().eq(ProcessDO::getProcess, process));
        if(list.isEmpty()){
            return true;
        }
        return false;
    }

    @ApiOperation(value = "获取(单个)")
    @GetMapping("/{id}")
    public ProcessDO getProcess(@PathVariable Long id) {
        return processService.getById(id);
    }

    @ApiOperation(value = "新增")
    @PostMapping("add")
    public Boolean saveProcess(@RequestBody ProcessDO processDO)
    {
        return processService.saveOrUpdate(processDO);
    }

    @ApiOperation(value = "删除(单个))")
    @DeleteMapping("/{id}")
    public Boolean deleteProcessById(@PathVariable Long id) {
        return processService.removeById(id);
    }
}
